package com.aitesting.controller;


import com.aitesting.common.CommonResult;
import com.aitesting.model.ApiGroup;
import com.aitesting.model.User;
import com.aitesting.service.ApiGroupService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 接口分组管理
 * @author zz
 * @since 2022-02-20
 */
@Slf4j
@RestController
@RequestMapping("/apiGroup")
public class ApiGroupController {

    @Autowired
    private ApiGroupService apiGroupService;

    /**
     * 根据id查询接口分组
     * @param id 分组id
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<ApiGroup> getById(@PathVariable("id") Integer id) {
        return CommonResult.success(apiGroupService.getById(id));
    }

    /**
     * 查询所有接口分组
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public CommonResult<ApiGroup> getList() {
        return CommonResult.success(apiGroupService.list());
    }

    /**
     * 新增接口分组
     * @param apiGroup 分组对象
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<ApiGroup> create(@Validated @RequestBody ApiGroup apiGroup, BindingResult result){
        boolean isOk = apiGroupService.save(apiGroup);
        if (isOk) {
            log.debug("创建分组成功 {}", apiGroup);
            return CommonResult.success(apiGroup);
        }else {
            log.error("新增分组失败,数据不合法 {}", apiGroup);
            return CommonResult.failed();
        }
    }

    /**
     * 根据id删除接口分组
     * @param id 接口分组id
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public CommonResult<ApiGroup> delete(@PathVariable Integer id) {
        boolean isOk = apiGroupService.removeById(id);
        if (isOk) {
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据id更新分组信息
     * @param apiGroup 分组对象
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public CommonResult<ApiGroup> update(@Validated @RequestBody ApiGroup apiGroup, BindingResult result) {
        boolean isOk = apiGroupService.updateById(apiGroup);
        if (isOk) {
            log.debug("更新分组成功 {}", apiGroup);
            return CommonResult.success(apiGroup);
        }else {
            log.error("更新分组失败,数据不合法 {}", apiGroup);
            return CommonResult.failed();
        }
    }

    /**
     * 根据项目id查询接口分组
     * @param projectId 项目id
     * @return CommonResult(code,message,data)
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<ApiGroup> list(@RequestParam(value = "projectId") Integer projectId) {
        QueryWrapper<ApiGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", projectId);
        return CommonResult.success(apiGroupService.list(queryWrapper));
    }
}

